/**
 * <pre>
 * Title: 		SMSListUtil.java
 * Project: 	AgentPortal_1.13.x
 * Author:		linriqing
 * Create:	 	2009-3-24 下午04:18:09
 * Copyright: 	Copyright (c) 2009
 * Company:		Shenzhen Helper
 * <pre>
 */
package cn.bisoft.java.util.common;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.hengpeng.common.configuration.SystemConfig;
import com.hengpeng.common.util.MD5;

/**
 * <pre>
 * 短消息工具
 * </pre>
 * @author linriqing
 * @version 1.0, 2009-3-24
 */
public class SMSUtil
{
	private final static Log logger = LogFactory.getLog(SMSUtil.class);

	/**
	 * 短信接口参数名-content
	 */
	public static final String EVENT_SMSPORT_PARANAME_CONTENT = "content";

	/**
	 * 短信接口参数名-flag
	 */
	public static final String EVENT_SMSPORT_PARANAME_FLAG = "flag";

	/**
	 * 短信接口参数名-phone
	 */
	public static final String EVENT_SMSPORT_PARANAME_PHONE = "phone";

	/**
	 * 短信接口参数名-phone
	 */
	public static final String EVENT_SMSPORT_PARANAME_DIGEST = "digest";

	/**
	 * 短信接口参数名-pwd
	 */
	public static final String EVENT_SMSPORT_PARANAME_PWD = "pwd";

	/**
	 * 短信接口参数名-username
	 */
	public static final String EVENT_SMSPORT_PARANAME_USERNAME = "username";

	/**
	 * 系统参数名-短信接口密码
	 */
	public static final String SYSTEMCONFIG_PARANAME_EVENT_SMSPORT_PWD = "SMSPORT_PWD";

	/**
	 * 系统参数名-短信接口地址
	 */
	public static final String SYSTEMCONFIG_PARANAME_EVENT_SMSPORT_URL = "SMSPORT_URL";

	/**
	 * 系统参数名-短信接口用户名
	 */
	public static final String SYSTEMCONFIG_PARANAME_EVENT_SMSPORT_USERNAME = "SMSPORT_USERNAME";

	/**
	 * <pre>
	 * 发送短信到手机上
	 * </pre>
	 * @param content 短信内容
	 * @param mobile 手机号码
	 */
	public static void doSendSMS(String content, String mobile)
	{
		if (!StringUtil.isEmpty(mobile))
		{
			String smsporturl = SystemConfig.getConfigString(SMSUtil.SYSTEMCONFIG_PARANAME_EVENT_SMSPORT_URL);
			String id = String.valueOf(System.currentTimeMillis());
			String timestamp = StringUtil.getStringFromDate(new Date(), "yyyyMMddHHmmss");
			String transMessage = createXmlString(id, timestamp, mobile, content);
			String paramenter = "transType=108&transMessage=" + transMessage;
			if ((smsporturl != null) && (smsporturl.length() > 0))
			{
				String smsRsp = SMSUtil.doPost(SystemConfig
						.getConfigString(SMSUtil.SYSTEMCONFIG_PARANAME_EVENT_SMSPORT_URL), paramenter, "UTF-8");
				System.out.println(smsRsp);
				logger.info("发送事件通知到手机号码{" + mobile + "}响应消息：" + smsRsp);
			}
		}
	}

	public static String doPost(String urlStr, String parameter, String requestEncode)
	{
		StringBuffer result = new StringBuffer();
		try
		{
			HttpURLConnection conn = (HttpURLConnection) new URL(urlStr).openConnection();
			conn.setConnectTimeout(5000);
			conn.setDoOutput(true);
			conn.setDoInput(true);
			conn.connect();
			OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), requestEncode);
			out.write(parameter);// 这里组织域信息
			out.flush();
			out.close();
			InputStream is = conn.getInputStream();
			BufferedReader reader = new BufferedReader(new InputStreamReader(is, requestEncode));
			String line = null;
			while ((line = reader.readLine()) != null)
			{
				result.append(line + "\n\r");
			}
			is.close();
			conn.disconnect();
			reader.close();
		}
		catch (Exception e)
		{
			logger.error(e);
		}
		return result.toString();
	}

	public static String createXmlString(String id, String timestamp, String mobile, String content)
	{
		String messageId = SystemConfig.getConfigString(SMSUtil.SYSTEMCONFIG_PARANAME_EVENT_SMSPORT_USERNAME);
		String passwd = SystemConfig.getConfigString(SMSUtil.SYSTEMCONFIG_PARANAME_EVENT_SMSPORT_PWD);
		String body = "<body><send level=\"0\" mobile=\"" + mobile + "\" content=\"" + content + "\"/></body>";
		String digestSource = messageId + timestamp + passwd + body;
		String xml = "<?xml version=\"1.0\" encoding=\"GBK\"?>";
		xml = xml + "<message version=\"1.0\" id=\"" + id + "\"><header>";
		xml = xml + "<messengerID>" + messageId + "</messengerID>";
		xml = xml + "<timestamp>" + timestamp + "</timestamp>";
		xml = xml + "<transactionType>108</transactionType>";
		xml = xml + "<digest>" + MD5.encodeString(digestSource, "UTF-8") + "</digest></header>";
		xml = xml + body + "</message>";
		return xml;
	}

	public static void main(String[] args) throws Exception
	{
		String mobile = "13510942204";
		String content = "科技化还";
		SystemConfig.setConfig(SMSUtil.SYSTEMCONFIG_PARANAME_EVENT_SMSPORT_USERNAME, "hp-jxsm");
		SystemConfig.setConfig(SMSUtil.SYSTEMCONFIG_PARANAME_EVENT_SMSPORT_PWD, "hp81666sm");
		SystemConfig.setConfig(SMSUtil.SYSTEMCONFIG_PARANAME_EVENT_SMSPORT_URL, "http://222.66.238.36/lotteryServlet");
		SMSUtil.doSendSMS(content, mobile);
	}
}
